package com.qj.yantaishan.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.common.annotation.Excel;
import com.qj.yantaishan.domain.ReturnProduct;
import com.qj.yantaishan.service.IReturnProductService;
import com.qj.yantaishan.vo.OrderReturnVo;
import com.qj.yantaishan.vo.ReturnOrderVo;
import com.qj.yantaishan.vo.ReturnProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.ReturnMapper;
import com.qj.yantaishan.domain.Return;
import com.qj.yantaishan.service.IReturnService;
import com.qj.yantaishan.common.core.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary
public class ReturnServiceImpl extends ServiceImpl<ReturnMapper, Return> implements IReturnService {
    @Autowired
    private ReturnMapper returnMapper;
    @Autowired
    private IReturnProductService returnProductService;

    @Override
    public Return getReturnOrderDetail(Integer orderId) {
        Return aReturn = getOne(new LambdaQueryWrapper<Return>().eq(Return::getId, orderId));
        List<ReturnOrderVo> returnOrderVos = returnMapper.selectReturnOrderList(orderId);
        List<ReturnProductVo> returnProductVos = returnMapper.selectReturnProductList(orderId);
        aReturn.setReturnOrderVos(returnOrderVos);
        aReturn.setReturnProductVos(returnProductVos);
        return aReturn;
    }

    @Override
    public Map<String, Object> getReturnList(Integer currentPage, Integer pageSize, String returnNo, String thirdId, Integer status) {
        Map<String, Object> map = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        int total = baseMapper.selectTotal();
        int pages = (total + pageSize - 1) / pageSize; // 16 + 9 / 10 12+9
        map.put("total", total);
        map.put("pages", pages);
        map.put("current", currentPage);
        map.put("size", pageSize);
        List<OrderReturnVo> returnVoList = baseMapper.selectOrderReturnList(current, pageSize, returnNo, thirdId, status);
        map.put("records", returnVoList);
        return map;

    }

    @Override
    public void addAudit(Map<String, String> audit) {
        String returnId = audit.get("returnId");
        String status = audit.get("status");
        final String storeContent = audit.get("storeContent");
        long time = new Date().getTime() / 1000;
        Return aReturn = baseMapper.selectById(returnId);
        if (aReturn == null) return;

        aReturn.setStatus(Integer.parseInt(status));
        aReturn.setCancelDateline(time);
        aReturn.setStoreContent(storeContent);
        //添加退款审核信息
        //1.根据status进行判断  2==拒绝退款  并携带  拒绝原因  storeContent
        baseMapper.updateById(aReturn);
    }

}
